package com.fitns.user.service.baseConfig;

import com.fitns.user.dao.baseConfig.WorkTimeDao;
import com.fitns.user.dto.baseConfig.WorkTimeDTO;
import com.fitns.user.dto.baseConfig.WorkTimeVO;
import com.fitns.user.exception.ServiceException;
import com.fitns.user.exception.WorkTimeMsgCode;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class WorkTimeService {

	@Autowired
	private WorkTimeDao workTimeDao;

    /**
     * 新增
     * @param workTimeDTO
     * @return
     */
	public WorkTimeVO insert(WorkTimeDTO workTimeDTO){
		if(Validator.isNull(workTimeDTO)){
			throw ServiceException.create(WorkTimeMsgCode.WORK_TIME_DATA_IS_NULL);
		}
        String nowDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Timestamp createDate=Timestamp.valueOf(nowDate);
		workTimeDTO.setInputTime(createDate);
		workTimeDTO.setModifyTime(createDate);
		workTimeDao.insert(workTimeDTO);
        WorkTimeVO workTimeVO = new WorkTimeVO();
        BeanUtils.copyProperties(workTimeDTO, workTimeVO);
		return workTimeVO;
	}

    /**
     * 修改
     * @param workTimeDTO
     * @return
     */
    public WorkTimeVO modify(WorkTimeDTO workTimeDTO) {
        if(Validator.isNull(workTimeDTO)){
            throw ServiceException.create(WorkTimeMsgCode.WORK_TIME_DATA_IS_NULL);
        }
        String nowDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Timestamp modifyDate=Timestamp.valueOf(nowDate);
        workTimeDTO.setModifyTime(modifyDate);
        workTimeDao.modify(workTimeDTO);
        WorkTimeVO workTimeVO = new WorkTimeVO();
        BeanUtils.copyProperties(workTimeDTO, workTimeVO);
        return workTimeVO;
    }

    /**
     * 查询列表
     * @param workTimeDTO
     * @return
     */
    public PageInfo<WorkTimeVO> selectList(WorkTimeDTO workTimeDTO) {
        List<WorkTimeVO> workTimeVOList = workTimeDao.selectList(workTimeDTO);
        PageInfo<WorkTimeVO> pageInfo=new PageInfo<>(workTimeVOList);
        return pageInfo;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public long delete(Long id){
        if(Validator.isNull(id)){
            throw ServiceException.create(WorkTimeMsgCode.WORK_TIME_ID_IS_NULL);
        }
        workTimeDao.delete(id);
        return id;
    }

    /**
     * 通过id从查询记录
     * @param id
     * @return
     */
    public WorkTimeVO selectById(Long id){
        return workTimeDao.selectById(id);
    }

}
